package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.vo.BusinessRecordVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    //商机跟进表
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    //商机表
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * 查询商机跟进记录
     * @param businessRecordVo
     * @return
     */
    @Override
    @Transactional
    public int add(BusinessRecordVo businessRecordVo) {
//        先修改 `商机表` 的基本信息  再去添加 `商机跟进表` 的数据

        TbBusiness business = new TbBusiness();

        //由于实体类中的字段大多相同可以使用  BeanUtils.copyProperties()  再设置个别字段
        //复制
        BeanUtils.copyProperties(businessRecordVo,business);

        //设置要修改的 商机id
        business.setId(businessRecordVo.getBusinessId());
        Date date = new Date();
        business.setCreateTime(date);

        //设置商机表中 的状态 为:2 进行中
        business.setStatus("2");

        tbBusinessMapper.updateTbBusiness(business);


        //获取当前登录用户
        String username = SecurityUtils.getUsername();
        //设置操作者
        businessRecordVo.setCreateBy(username);

        return tbBusinessTrackRecordMapper.add(businessRecordVo);
    }

    @Override
    public List<BusinessRecordVo> list(Long id) {
        return tbBusinessTrackRecordMapper.list(id);
    }
}
